package com.yyk.yykywd.util;


import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;

import androidx.appcompat.app.AlertDialog;

import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import android.widget.TextView;


import com.yyk.yykywd.R;

import com.yyk.yykywd.listenter.DialogSureCancelListenter;
import com.yyk.yykywd.listenter.OneValueListenter;
import com.yyk.yykywd.listenter.TakePhotoListenter;


/**
 * Created by Administrator on 2018/9/19 0019.
 */

public class DialogUtil {
    /**
     * android 点击 头像对应的 底部弹出框拍照，相册取消 简单实现 - CSDN博客  https://blog.csdn.net/bbtianshi/article/details/81224736
     * crazycodeboy/TakePhoto: 一款用于在Android设备上获取照片（拍照或从相册、文件中选择）、裁剪图片、压缩图片的开源工具库  https://github.com/crazycodeboy/TakePhoto
     */

    public static void showTakePhoto(Context context, final TakePhotoListenter listenter) {
        Dialog dialog = new Dialog(context, R.style.Dialog_xiangjixiangce);
        //填充对话框的布局
        View inflate = LayoutInflater.from(context).inflate(R.layout.dialog_xiangjixiangce, null);
        //初始化控件
        TextView camera = (TextView) inflate.findViewById(R.id.camera);
        TextView pic = (TextView) inflate.findViewById(R.id.pic);
        TextView cancel = (TextView) inflate.findViewById(R.id.cancel);
        camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listenter.onCamera();

                dialog.dismiss();


            }
        });
        pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listenter.onPic();

                dialog.dismiss();
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        //将布局设置给Dialog
        dialog.setContentView(inflate);
        //获取当前Activity所在的窗体
        Window dialogWindow = dialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        //从底部来一个动画  已经style设置上了
//        dialogWindow.setWindowAnimations(R.style.mystyle);

        dialog.show();//显示对话框

        Window win = dialog.getWindow();
        win.getDecorView().setPadding(0, 0, 0, 0);
        //获得窗体的属性
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.y = 20;//设置Dialog距离底部的距离
//       将属性设置给窗体
        dialogWindow.setAttributes(lp);
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        win.setAttributes(lp);
    }

    /**
     * 原生提示框
     * <p>
     * 不需要返回的时候  true 外部不能点击
     */

    public static void showReturnTips(Context context, String title, String message, String sureTitle, String cancelTitle, final DialogSureCancelListenter listenter, boolean flag) {
        final AlertDialog alertDialog = new AlertDialog.Builder(context)
                .setTitle(title)
                .setMessage(message)
                .setPositiveButton(sureTitle, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        listenter.onSureListenter();

                    }
                })
                .setNegativeButton(cancelTitle, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        listenter.onCancelListenter();

                    }
                }).create();
        if (flag) {
            //实现点击外部不消失。
            alertDialog.setCanceledOnTouchOutside(false);
            // 在弹出的时候禁止使用返回键
            alertDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    if (alertDialog != null && alertDialog.isShowing()) {
                        return true;
                    }
                    return false;
                }
            });
        }
        alertDialog.show();
        alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(0xff6191F4);
        alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(0xff6191F4);
//        return alertDialog;
    }
}
